/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */

import { noop } from '../../../../common/util/noop.js';
import { observe } from '../../../../common/util/observable-property.js';
import { HvigorLogger } from '../../../log/hvigor-log.js';

/**
 * hvigorfile.ts的默认导出
 * export default中定义的配置项
 */
export interface LoaderProfile {
  system: Function;
  plugins: Function[];
  config: Record<string, any>;
}

/**
 * LoaderProfile对象
 */
export interface Config {
  getObject: (name: string) => any;
  setObject: (name: string, obj: object) => void;
}

export class CoreConfig implements Config {
  private logger: HvigorLogger = HvigorLogger.getLogger(CoreConfig.name);
  private config: any;

  constructor(config: any) {
    if (!config) {
      this.config = observe({}, noop);
      return;
    }
    this.config = observe(config, noop);
  }
  getObject(name: string): any {
    return this.config ? this.config[name] : undefined;
  }

  setObject(name: string, obj: any): void {
    Object.keys(this.config[name]).forEach((key) => delete this.config[name][key]);
    Object.keys(obj).forEach((key) => {
      this.config[name][key] = observe(obj[key], noop);
    });
  }
}
